package com.enterprise.support.utility.generator.panel;

import com.enterprise.application.swing.jeta.AbstractPanel;
import com.enterprise.support.utility.Enterprise;
import com.enterprise.support.utility.generator.pub.GeneratorConfig;
import com.enterprise.support.utility.generator.pub.FreemarkerManager;
import com.enterprise.support.utility.SwingUtility;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.Date;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2011-2-9
 * Time: 11:27:40
 * To change this template use File | Settings | File Templates.
 */
public class TabSpring extends AbstractPanel {
    private final static File srcDirectory = new File(com.enterprise.support.utility.Configuration.LibDirectory, "/java/src");
    private final static File comDirectory = new File(srcDirectory, "com");
    private final static DefaultTableModel tableModel = new DefaultTableModel(new Vector() {{
        add("接口");
        add("实现类");
    }}, 0);
    private static DefaultComboBoxModel modulesComboBoxModel;
    private static DefaultComboBoxModel domainsComboBoxModel;

    public TabSpring() {
        super("com\\enterprise\\support\\utility\\generator\\panel\\TabSpring.xml");
    }

    public void initComponents() {
        mainPanel.getComboBox("projects").setModel(new DefaultComboBoxModel(new Vector<Object>() {{
            File[] files = comDirectory.listFiles();
            if (null != files) {
                for (File project : files) {
                    if (project.isDirectory()) {
                        add(project.getName());
                    }
                }
            }
        }}));

        modulesComboBoxModel = new DefaultComboBoxModel(new Vector<Object>() {{
            File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application");
            File[] files = moduleDirectory.listFiles();
            if (null != files) {
                for (File module : files) {
                    if (module.isDirectory()) {
                        add(module.getName());
                    }
                }
            }
        }});

        mainPanel.getComboBox("modules").setModel(modulesComboBoxModel);
        domainsComboBoxModel = new DefaultComboBoxModel(new Vector<Object>() {{
            add(" 请选择");
            File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application/" + mainPanel.getComboBox("modules").getSelectedItem() + "/domain");
            File[] files = moduleDirectory.listFiles();
            if (null != files) {
                for (File domain : files) {
                    if (domain.isFile() && domain.getName().endsWith("java")) {
                        add(" " + domain.getName());
                    }
                }
            }
        }});
        mainPanel.getComboBox("domains").setModel(domainsComboBoxModel);

        mainPanel.getTable("results").setModel(tableModel);
        final File logicDirectory = new File(GeneratorConfig.homeDirectory, "/java/src/com/" + mainPanel.getComboBox("projects").getSelectedItem() + "/application/" + mainPanel.getComboBox("modules").getSelectedItem() + "/logic/");
        File[] files = logicDirectory.listFiles();
        tableModel.getDataVector().clear();
        if (files != null) {
            for (final File file : files) {
                if (file.isFile()) {
                    tableModel.addRow(new Vector() {{
                        add(file.getName());
                        add(new File(logicDirectory, "impl/" + new StringBuffer(file.getName().substring(1)).insert(file.getName().indexOf(".java") - 1, "Impl")).getName());
                    }});
                }
            }
        } else {
            tableModel.addRow(new Vector());
        }
    }

    public void addListeners() {
        mainPanel.getComboBox("projects").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                modulesComboBoxModel.removeAllElements();
                File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application");
                File[] files = moduleDirectory.listFiles();
                if (files != null) {
                    for (File module : files) {
                        if (module.isDirectory()) {
                            modulesComboBoxModel.addElement(module.getName());
                        }
                    }
                }
            }
        });
        mainPanel.getComboBox("modules").addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent event) {
                if (event.getStateChange() == 1) {
                    domainsComboBoxModel.removeAllElements();
                    domainsComboBoxModel.addElement(" 请选择");
                    File moduleDirectory = new File(new File(comDirectory, String.valueOf(mainPanel.getComboBox("projects").getSelectedItem())), "application/" + mainPanel.getComboBox("modules").getSelectedItem() + "/domain");
                    File[] files = moduleDirectory.listFiles();
                    if (null != files) {
                        for (File domain : files) {
                            if (domain.isFile() && domain.getName().endsWith("java")) {
                                domainsComboBoxModel.addElement(" " + domain.getName());
                            }
                        }
                    }

                    final File logicDirectory = new File(GeneratorConfig.homeDirectory, "/java/src/com/" + mainPanel.getComboBox("projects").getSelectedItem() + "/application/" + mainPanel.getComboBox("modules").getSelectedItem() + "/logic/");
                    files = logicDirectory.listFiles();
                    tableModel.getDataVector().clear();
                    if (files != null) {
                        for (final File file : files) {
                            if (file.isFile()) {
                                tableModel.addRow(new Vector() {{
                                    add(file.getName());
                                    add(new File(logicDirectory, "impl/" + new StringBuffer(file.getName().substring(1)).insert(file.getName().indexOf(".java") - 1, "Impl")).getName());
                                }});
                            }
                        }
                    } else {
                        tableModel.addRow(new Vector());
                    }
                }
            }
        });
        mainPanel.getButton("submit").addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final Enterprise input = getInput();
                if (!input.equals("domains", " 请选择") && input.isEmpty("classname")) {
                    input.set("classname", input.getString("domains").replace("Entity", "").replace(".java", ""));
                }
                if (input.isEmpty("classname")) {
                    SwingUtility.warning("请选择 实体类 或 输入类名称！");
                } else {

                    input.set("date", new Date());
                    input.set("domains", input.getString("domains").trim().replace(".java", ""));
                    input.set("classname", input.getString("classname").trim().toLowerCase());
                    input.set("interfaceName", "I" + input.getString("classname").replaceFirst("[a-z]", input.getString("classname").substring(0, 1).toUpperCase()));
                    input.set("className", input.getString("classname").replaceFirst("[a-z]", input.getString("classname").substring(0, 1).toUpperCase()) + "Impl");


                    File domainDirectory = new File(GeneratorConfig.homeDirectory, "/java/src/com/" + input.getString("projects") + "/application/" + input.getString("modules") + "/logic/");
                    String ILogic = new File(domainDirectory, input.getString("interfaceName") + ".java").getAbsolutePath();
                    String LogicImpl = new File(domainDirectory, "impl/" + input.getString("className") + ".java").getAbsolutePath();

                    for (Object values : tableModel.getDataVector()) {
                        if (input.equals("interfaceName", String.valueOf(((Vector) values).get(0)).replace(".java", ""))) {
                            if (SwingUtility.confirm(String.valueOf(((Vector) values).get(0)).replace(".java", "") + " 已存在,是否需要覆盖！") != JOptionPane.YES_OPTION) {
                                return;
                            }
                        }
                    }

                    FreemarkerManager.builerTemplate("ILogic.ftl", input, ILogic);
                    FreemarkerManager.builerTemplate("LogicImpl.ftl", input, LogicImpl);
                    SwingUtility.warning("实体对象已生成！");


                    for (Object values : tableModel.getDataVector()) {
                        if (input.equals("interfaceName", String.valueOf(((Vector) values).get(0)).replace(".java", ""))) {
                            return;
                        }
                    }
                    tableModel.addRow(new Vector() {{
                        add(input.getString("interfaceName") + ".java");
                        add(input.getString("className") + ".java");
                    }});
                }
            }
        });
    }
}
